home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / HUGS1 / hs / arrayEx < prev    next >
Text File  |  1995-02-14  |  983b  |  24 lines

  1. -- Some simple examples using arrays.  Requires array.gs.
  2.  
  3. -- Some applications, most taken from the Gentle Introduction ... -------------
  4.  
  5. timesTable :: Array (Int,Int) Int
  6. timesTable  = array ((1,1),(10,10)) [ (i,j) := i*j | i<-[1..10], j<-[1..10] ]
  7.  
  8. fibs n = a where a = array (0,n) ([ 0 := 1, 1 := 1 ] ++
  9.                                   [ i := a!(i-2) + a!(i-1) | i <- [2..n] ])
  10.  
  11. wavefront n = a where a = array ((1,1),(n,n))
  12.                              ([ (1,j) := 1 | j <- [1..n] ] ++
  13.                               [ (i,1) := 1 | i <- [2..n] ] ++
  14.                               [ (i,j) := a!(i,j-1) + a!(i-1,j-1) + a!(i-1,j)
  15.                                            | i <- [2..n], j <- [2..n] ])
  16.  
  17. listwave n = [ [wf!(i,j) | j <- [1..n]] | i <- [1..n] ]
  18.              where wf = wavefront n
  19.  
  20. eg1 :: Array Integer Integer
  21. eg1  = array (1,100) ((1 := 1) : [ i := i * eg1!(i-1) | i <- [2..100] ])
  22.  
  23. -------------------------------------------------------------------------------
  24.